<?php

namespace app\controller;

use app\BaseController;
use app\message;
use app\model\SystemOperateLog;
use app\model\SystemSign;
use app\model\SystemTargetLib;
use app\Request;

/**
 * 系统签到表 用于确定工作日
 */
class SystemSignIn extends BaseController
{
    /**
     * 签到
     * 如果已经签到过，则取消签到
     */
    public function sign(Request $request,SystemSign $systemSignModel,SystemOperateLog $systemOperateLogModel)
    {
        $uid = $request->post ('user_login_id');

        //获取今日起始时间
        $dayDate = date('Y-m-d').' 00:00:00';

        $daySign = $systemSignModel->where ('create_date',$dayDate)->findOrEmpty ();

        if (!$daySign->isEmpty ()){
            //已经签到过
            $systemSignModel->where ('create_date',$dayDate)->delete ();

            //记录日志
            $systemOperateLogModel->addOperate ($uid,message::$SYSTEM,'取消签到 :'.$dayDate);
            return ReJson (message::$SUCC_CODE,message::$DAY_SIGN_SUCC_CANCEL);
        }
        //发起签到
        $systemSignModel->insert(['uid'=>$uid,'create_date'=>$dayDate]);
        $systemOperateLogModel->addOperate ($uid,message::$SYSTEM,'签到成功 :'.$dayDate);
        return ReJson (message::$SUCC_CODE,message::$DAY_SIGN_SUCC);
    }

    /**
     * 查询今天是否签到
     */
    public function getSignStatus(SystemSign $systemSignModel)
    {
        //获取今日起始时间
        $dayDate = date('Y-m-d').' 00:00:00';

        $daySign = $systemSignModel->where ('create_date',$dayDate)->findOrEmpty ();

        if (!$daySign->isEmpty ()){
            return ReJson (message::$SUCC_CODE,message::$SUCC_SELECT_DATA,1);//1未签到
        }
        return ReJson (message::$SUCC_CODE,message::$SUCC_SELECT_DATA,2);//2已签到
    }

    /**
     * 首页签到情况
     */
    public function getIndexSignData(Request $request,SystemSign $systemSignModel,SystemTargetLib $targetLibModel)
    {
        $uid = $request->get ('user_login_id');
        //获取今日起始时间
        $dayDate = date('Y-m-d').' 00:00:00';
        //今日签到情况
        $daySign = $systemSignModel->where ('create_date',$dayDate)->findOrEmpty ();

        if ($daySign->isEmpty ()){
            $daySignStatus = 1; //1未签到
        }else{
            $daySignStatus = 2; //已签到
        }

        //当月签到总数
        $startDate = date("Y-m-01")." 00:00:00";
        $endData = date("Y-m-t")." 23:59:59";
        $moonSignSum = $systemSignModel->whereBetweenTime ('create_date',$startDate,$endData)->count();

        //当月工作日
        $moon_day = $targetLibModel->getValueTargetDayModel(date('Y-m'),$uid,3,3)?:0;

        return ReJson (message::$SUCC_CODE,message::$SUCC_SELECT_DATA,[
            'today_sign_status'=>$daySignStatus,
            'moon_sign_sum'=>$moonSignSum,
            'moon_day'=>$moon_day
        ]);

    }

}